package cn.lanqiao.bigDate.service.data.impl;

import cn.lanqiao.bigDate.entity.data.DataUser;
import cn.lanqiao.bigDate.mapper.data.DataUserMapper;
import cn.lanqiao.bigDate.service.data.DataUserService;
import cn.lanqiao.common.utils.EntityUtil;
import cn.lanqiao.common.utils.MD5Util;
import cn.lanqiao.common.utils.RandomUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.val;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author Marisa
 * @description 前端用户管理实现类
 * @date 2022/3/29
 */
@Service
public class DataUserServiceImpl
extends ServiceImpl<DataUserMapper, DataUser>
implements DataUserService {

    @Resource
    private DataUserMapper dataUserMapper;

    @Override
    public IPage<DataUser> findByPage(Page<DataUser> page, String str) {
        val param = JSONObject.parseObject(str, Map.class);
        String keyword = (String) param.get("keywords");
        String status = (String) param.get("status");
        QueryWrapper<DataUser> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("id");
        if(!keyword.equals("")){
            wrapper.and(e -> {
                e.like("user_name", keyword).or();
                e.like("login_account", keyword).or();
                e.like("telephone", keyword);
            });
        }
        if(!status.equals("-1")){
            wrapper.eq("status", status);
        }
        Page<DataUser> selectPage = dataUserMapper.selectPage(page, wrapper);
        List<DataUser> records = selectPage.getRecords();
        records.forEach(e -> e.setPassword(""));
        return selectPage;
    }

    @Override
    public DataUser findDataUserOne(Long id) {
        QueryWrapper<DataUser> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        DataUser dataUser = dataUserMapper.selectOne(wrapper);
        EntityUtil<DataUser> entityUtil = new EntityUtil<>();
        return entityUtil.setColumnValue(dataUser, "password", "");
    }

    private DataUser getDataUserById(Long id) {
        QueryWrapper<DataUser> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        return dataUserMapper.selectOne(wrapper);
     }

    @Override
    public Boolean saveOrUpdateBatchDataUser(List<DataUser> userList) {
        userList.forEach(e -> {
            Long id = e.getId();
            LocalDateTime now = LocalDateTime.now();
            DataUser dataUserOne = null;
            String pwd = e.getPassword();
            if(id == null){ // id为空，是创建用户
                e.setCreateTime(now);
                String salt = RandomUtil.getRandom(4);
                e.setSalt(salt);
                pwd = MD5Util.md5(e.getPassword() + salt);
            }else {
                dataUserOne = getDataUserById(id);
            }
            if(id != null){ // id不为空，更新用户
                if(pwd == null || pwd.equals("")){
                    pwd = dataUserOne.getPassword();
                }else {
                    String pwdStr = e.getPassword() + dataUserOne.getSalt();
                    pwd = MD5Util.md5(pwdStr);
                }
            }
            e.setPassword(pwd);
            e.setUpdateTime(now);
        });
        DataUserService dataUserService = new DataUserServiceImpl();
        return dataUserService.saveOrUpdateBatch(userList);
    }

    @Override
    public Boolean deleteDataUserBatch(String ids) {
        String[] split = ids.split(",");
        List<String> idList = Arrays.asList(split);
        if (idList.isEmpty()) {
            return false;
        }
        int i = dataUserMapper.deleteBatchIds(idList);
        System.out.println(i);
        return i != -1;
    }

    @Override
    public Boolean deleteDataUserBatch(List<Long> idList) {
        if (idList.isEmpty()) {
            return false;
        }
        int i = dataUserMapper.deleteBatchIds(idList);
        return i != -1;
    }

    public static void main(String[] args) {

    }
}
